package com.lv.phuotbacnam.ui.fragment;

//package com.lv.phuotbacnam.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.log.Logger;
import com.lv.phuotbacnam.bl.log.Toaster;
import com.lv.phuotbacnam.bl.network.util.location.LocationHelper;
import com.lv.phuotbacnam.ui.Constants;
import com.lv.phuotbacnam.ui.Section;
import com.lv.phuotbacnam.ui.activity.MainActivity;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

/**
 * Splash screen fragment
 */
public class SplashFragment extends BaseFragment {

	/**
	 * Splash screen duration in millis
	 */
	private static final long SPLASH_DURATION_MILLIS = 3000;

	private Handler mHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_splash, container,
				false);
		

		return rootView;
	}

	@Override
	public void onResume() {
		super.onResume();

		checkLocationStatus();

	}

	private void checkLocationStatus() {
		Logger.logMessage("checkLocationStatus");
		LocationHelper locationHelper = new LocationHelper(getActivity());
		if (locationHelper.checkLocationTurned()) {
			Location lc = locationHelper.getLocation();
			if (lc == null)
				Toaster.showToast(getActivity(),
						getResources().getString(R.string.khong_the_lay_vi_tri));
			else {
				Logger.logMessage("lat:" + lc.getLatitude() + " -long:"
						+ lc.getLongitude());

				Constants.CURRENT_LOCATION = lc;
			}

			checkFacebookLogin();

		} else {

			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
					getActivity());
			alertDialogBuilder.setCancelable(false);
			alertDialogBuilder.setMessage(
					getResources().getString(
							R.string.ban_phai_bat_quyen_truy_cap_vi_tri))
					.setPositiveButton(
							getResources().getString(R.string.dong_y),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									Intent callGPSSettingIntent = new Intent(
											android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									getActivity().startActivity(
											callGPSSettingIntent);
								}
							});
			AlertDialog alert = alertDialogBuilder.create();
			alert.show();
		}
	}

	/**
	 * TO check Fb login status
	 */
	private void checkFacebookLogin() {
		mHandler = new Handler();
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				isFacebookLogged();
			}
		}, SPLASH_DURATION_MILLIS);
	}

	/**
	 * To check Facebook login status
	 */
	private void isFacebookLogged() {
		// if it haven't logged facebook yet
		if (!isDataExist())
			getNavigationController().navigateTo(StartFragment.class,
					Section.INVITE, null, false);
		else
			getNavigationController().navigateTo(MainActivity.class, true,
					Section.INVITE, null);
	}

	private boolean isDataExist() {
		com.facebook.Session session = ParseFacebookUtils.getSession();
		if (session != null && session.isOpened()
				&& ParseUser.getCurrentUser().getUsername() != null)
			return true;
		return false;
	}

	// Test svn
}
